﻿// 声明一个全局对象Namespace，用来注册命名空间
/// <reference path="jquery-1.4.1-vsdoc.js" />

var Namespace;
if (typeof Namespace == "undefined" || !Namespace) {
    Namespace = new Object();
}
// 全局对象仅仅存在register函数，参数为名称空间全路径，如"Grandsoft.GEA"
Namespace.register = function (fullNS) {
    // 将命名空间切成N部分, 比如Grandsoft、GEA等
    var nsArray = fullNS.split('.');
    var sEval = "";
    var sNS = "";
    for (var i = 0; i < nsArray.length; i++) {
        if (i != 0) sNS += ".";
        sNS += nsArray[i];
        // 依次创建构造命名空间对象（假如不存在的话）的语句
        // 比如先创建Grandsoft，然后创建Grandsoft.GEA，依次下去
        sEval += "if (typeof(" + sNS + ") == 'undefined') " + sNS + " = new Object();"
    }
    if (sEval != "") eval(sEval);
}


$(function () {
    // 按钮操作
    $(":button[url]").click(function () {
        try {
            document.location.assign($(this).attr("url"));
        } catch (e) { }
    });

    // 左右, 上一页,下一页
    $(document).keyup(function (event) {
        $wwtfly.boxPageNext(event);
    });

});

// 行操作
var $wwtflyTr = {
    backColor: "#FFD58D",
    check: function () {
    },
    mouseOver: function () {
        $(this).css({ backgroundColor: $wwtflyTr.backColor });
    },
    mouseOut: function () {
        $(this).css({ backgroundColor: "" });
    },
    click: function () {
        var s = $(this);
        s.find(":checkbox[name='cbKey'],:radio[name='cbKey1']").each(function()
        {
            $(this).attr("checked", !$(this).attr("checked"));
        });
        if (s.find(":checkbox[name='cbKey'],:radio[name='cbKey1']").attr("checked")) {
            s.unbind("mouseover").unbind("mouseout").css("background-color", $wwtflyTr.backColor);
        } else {
            s.bind({
                mouseover: $wwtfly.delegate(this, $wwtflyTr.mouseOver),
                mouseout: $wwtfly.delegate(this, $wwtflyTr.mouseOut)
            }).css("background-color", "");
        }
    }
};

 // 公共方法
var $wwtfly = {

    delegate: function (context, handler) { // 绑定事件
        return function (e) {
            handler.apply(context, [e, this]);
        };
    },
    stop: function (handler, context) { // 阻止冒泡
        return function (e) {
            e.stopPropagation();
            handler.apply(context || this, arguments);
        };
    },
    stopAll: function (handler, context) { // 阻止 all
        return function (e) {
            e.preventDefault();
            e.stopPropagation();
            handler.apply(context || this, arguments);
        }
    },
    /*获取高度..*/
    getCurrentHeight: function (ifrmaeObj) {
        var height, scrollHeight, offsetHeight;
        if (document.height) {
            height = document.height;

        } else if (document.body) {
            if (document.body.scrollHeight) {
                height = scrollHeight = document.body.scrollHeight;
                //alert("document.body.scrollHeight:" + document.body.scrollHeight);
            }

            if (document.body.offsetHeight) {
                height = offsetHeight = document.body.offsetHeight;
                //alert("document.body.offsetHeight:" + document.body.offsetHeight);
            }

            if (scrollHeight && offsetHeight) {
                height = Math.max(scrollHeight, offsetHeight);
            }
        }
        return height;
    },
    /*获取高度..(ifrmaeObj 原生的iframe对象)*/
    getCurrentHeightOfIframe: function (ifrmaeObj) {
        var iframeWindow = ifrmaeObj.contentWindow;
        var height, scrollHeight, offsetHeight;
        if (iframeWindow.document.height) {
            height = iframeWindow.document.height;

        } else if (iframeWindow.document.documentElement) {
            if (iframeWindow.document.documentElement.scrollHeight) {
                height = scrollHeight = iframeWindow.document.documentElement.scrollHeight;
            }

            if (iframeWindow.document.documentElement.offsetHeight) {
                height = offsetHeight = iframeWindow.document.documentElement.offsetHeight;
                //alert("document.body.offsetHeight:" + document.body.offsetHeight);
            }

            if (scrollHeight && offsetHeight) {
                height = Math.max(scrollHeight, offsetHeight);
            }
        }
        return height;
    },
    preventDefault: function (e) { // 阴止默认行为
        e.preventDefault();
    },
    trigger: function (element, eventName, e) { // 事件触发器
        e = $.extend(e || {}, new $.Event(eventName));
        e.stopPropagation();
        $(element).trigger(e);
        return e.isDefaultPrevented();
    },
    renderTable: function (table, alterCssName) { //变色
        table.find("tbody > tr").each(function () {
            $(this).find(":checkbox[name='cbKey'],:radio[name='rdKey']").click(function () {
                $(this).attr("checked", !$(this).attr("checked"));
            }).end().bind({
                click: $wwtfly.delegate(this, $wwtflyTr.click),
                mouseover: $wwtfly.delegate(this, $wwtflyTr.mouseOver),
                mouseout: $wwtfly.delegate(this, $wwtflyTr.mouseOut)
            });
        }).end().find("tbody > tr:odd").addClass(alterCssName);
    },
    boxPageNext: function (event) { //左右,上一页 下一页
        if (event != null) {
            var keyCode = event.keyCode;
            if (keyCode == 37) {
                var prevLiUrl = $(".box_page li.current").prev("li").find("a").attr("href");
                if (prevLiUrl != undefined && prevLiUrl != document.location.pathname) {
                    document.location.assign(prevLiUrl);
                }
            } else if (keyCode == 39) {
                //找到当前页
                var nextLiUrl = $(".box_page li.current").next("li").find("a").attr("href");
                if (nextLiUrl != undefined && nextLiUrl != document.location.pathname) {
                    document.location.assign(nextLiUrl);
                }
            }
        }
    }
}
